/**************************************************************************
 * @doc ICIC
 * @module ICICPRNT.C |
 * Source code for the ICIC Receipt handling functions.
 *
 * Product         :   SoftPay 2000 (Global Payment Methodology).
 * <nl>Developer   :   SoftPay Development Team (I_SP2000).
 * <nl>Notes       :
 *
 * @head3 ICIC Functions |
 * @index | ICIC
 * @end
 *
 * Copyright (c) 1996-2000 by VeriFone Inc. All rights reserved.
 *
 * No part of this software may be used, stored, compiled, reproduced,
 * modified, transcribed, translated, transmitted, or transferred, in any form
 * or by any means whether electronic, mechanical, magnetic, optical,
 * or otherwise, without the express prior written permission of VeriFone, Inc.
 *
 * Revision History
 * ------------------------------------------------------------------------
 $Log: /PROJECTS/ICIC-EMV/Source/SPK2SIM/Source/EMVSrc/EMVPrnt.c $
 *
 * 3     3/26/04 2:34p Venu_v1
 * Adding the ATN in printing...
 ********************************************************************************/
#include <project.h>
#define CPAC_CPKPRINT_C

#include <aclconio.h>
#include <txoinc.h>
#include <ascii.h>
#include <errno.h>
#include <applidl.h>
#include <printer.h>
#include <formater.h>
#include <message.h>
#include <stdlib.h>
#include <aclstr.h>
#include <string.h>
#include <actutil.h>
#include <acldev.h>

#include <define.h>
#include <sizes.h>
#include <transact.h>
#include <print.h>
#include <gds.h>
#include <lct.h>
#include <tct.h>
#include <pct.h>
#include <iit.h>
#include <ui.h>
#include <msg.h>
#include <msgfuncs.h>
#include <form.h>
#include <stats.h>
#include <paramrpt.h>
#include <logo.h>
#include <tran.h>
#include <hdt.h>
#include <report.h>
#include <mit.h>
#include <msgclrk.h>
#include <report.h>
#include <pform.h>
#include <format.h>
#include <settlbat.h>
#include <multilng.h>
#include <settle.h>
#include <clrkrpts.h>
#include <date.h>
#include <funcids.h>
#include <batmsg.h>
#include <trt.h>
#include <pbatch.h>
#include <aclstr.h>
#include <batch.h>
#include <tblutil.h> //CPAC2.1B
//#include "cpacoper.h"
//#include "cpacmsg.h"
#include <spdebug.h>
#include <init.h>
#include <util.h>
#include <confio.h>
#include <oper.h>
#include <entry.h>
#include <file.h>
#include <card.h>
#include <date.h>
#include <accum.h>
#include <accmsg.h>
#include <rcptsegs.h>
#include <rcptcmpt.h>
#include <udt.h>
#include <table.h>
#include <ifldtbl.h>
#include <..\cpacsrc\cpacids.h>


//#include "cpacsize.h"
//#include <..\cpacsrc\cpacprnt.h>
//#include "sqautils.h"
#include "EMVprnt.h"

#ifdef AMEX_SOC

#include "..\axapsrc\axapids.h"

#endif /* AMEX_SOC */

#ifdef DOWNLOAD_MULTIPLE_LOGO
extern int inMultipleDownloadLogo(int);
#endif /* DOWNLOAD_MULTIPLE_LOGO */


static unsigned char szMyScratch1[30];
int inEMVPrintReceiptTop(TRANSACTION_OBJECT *pobTran)
{

	#ifdef CITI_PHIL
	//char szTime1[TIME_SIZE + 1];
	#endif /* CITI_PHIL */
	LOG_PRINTFF((0x8L,"Print start"));
	if (pobTran->fPreprinted)
	return(VS_SUCCESS);

	if (inGetPrinterType() == NOPRINTER  && fGetEnablePrinter())
	{
	vdDisplayMessageFromFile( NO_PRINTER_MSG, WARNING_MSG);
	return(VS_SUCCESS);
	}
	if (!pobTran->srTRTRec.PrintReceipt && !(pobTran->srBRec.inPrintOption & PRT_DUPLICATE))
	return(VS_SUCCESS);

	if (((inGGCode() == PRE_COMP) && (!fPrintCloseTab(pobTran))) ||
	((inGGCode() == PRE_AUTH) && (fGetOpenTabOptRec() == VS_FALSE)))
	{
	vdDisplayMessageFromFile( NO_RCPT_ERR_MSG, WARNING_MSG);
	pause(50);
	return(VS_SUCCESS);
	}

	if (fGetEnablePrinter())
	vdDisplayMessageFromFile( PRINTING_MSG, STATUS_MSG);

	/*
	Preprint up to account number
	*/

	inPrintReceiptHeader (pobTran);

	/* LENIN CPAC #if TBD_CPAC */
	vdSetPrintReceiptConditions(pobTran);
	/* LENIN CPAC #endif  CPAC_TBD */

	/*LENIN CPAC  #if TBD_CPAC */
	vdMakeReceiptTop(pobTran);
	/* LENIN #endif  CPAC_TBD */

	vdEMVMakeReceiptAfterResp(pobTran);
	pad (srRcpt.unFrm.srReceipt.szSTAN, srRcpt.unFrm.srReceipt.szSTAN, '0', 6, RIGHT);
	pad (srRcpt.szInvNum, srRcpt.szInvNum, '0', 6, RIGHT);  // Inv_num padding CPAC_CITI_PHIL

	#ifdef CITI_PHIL
	//Kelvin_F2 --- start: no need to mask MID
	#ifdef MASK_TID_AND_MID
	vdMaskCitiMTID();
	#endif
	//Kelvin_F2 --- end: no need to mask MID
	#endif /* CITI_PHIL */

	/* LENIN CPAC #if TBD_CPAC */
	inPrintReceiptHeader2(&obPrinter, pobTran);
	/* LENIN CPAC #endif  CPAC_TBD */

	pobTran->fPreprinted = VS_TRUE;


	///*Saritha_b1.............Start...........Amex Roc Printing */
	//#ifdef AMEX_SOC

	//	strcpy(srRcpt.unFrm.srReceipt.szGenBuf2, (char*)srISO.pszField62);

	//if (inPrintLines(&obPrinter, AMEX_SOC_PRINT_START, AMEX_SOC_PRINT_END, NO_BL_LINE, 0L) != VS_SUCCESS)
	//	return(VS_ERROR);

	//#endif  /*   AMEX_ROC */

	/*Saritha_b1.............End...........Amex Roc Printing */
	return(VS_SUCCESS);
}

void vdEMVMakeReceiptAfterResp(TRANSACTION_OBJECT *pobTran)
{

	int len;
	char szPan[PAN_SIZE + 1], acct[PAN_SIZE + 1];//, szDate[DATE_SIZE + 1];
	short shTempSugTipPer1,shTempSugTipPer2, shTempSugTipPer3;
	long lnSChg = 0L, lnBaseAmt, lnSubTotAmt, lnTipAmt1 , lnTipAmt2 , lnTipAmt3 ;
	unsigned char szMyScratch[30];
       memset(szMyScratch, 0x0, sizeof(szMyScratch));

	/*if((pobTran->inTransactionCode == CPAC_PHIL_ADJUST)||(pobTran->inTransactionCode == ADJUST))
	{
		vdGetDate(szDate,MMDDYY);
		strlcpy(srRcpt.szDate,&(szDate[4]),2);
		strlcpy(srRcpt.szDate+2,pobTran->srBRec.szDate,2);
		strlcpy(srRcpt.szDate+4,&(pobTran->srBRec.szDate[2]),2);
		vdFormatDate(srRcpt.szDate);
		strcpy(srRcpt.szTime,pobTran->srBRec.szTime);
		vdFormatTime(srRcpt.szTime);
	}
	else*/
		vdSetReceiptDateAndTime(pobTran);
	   inGetBatchFieldData (&pobTran->srBRec, CITI_PIP_TRANS_CODE, szMyScratch, 19);
	//inGetTranFlexiData(pobTran, CITI_PIP_TRANS_CODE, szMyScratch, 19);
	if(strlen((char *)szMyScratch) >0)
	{
        memset(szMyScratch1,'\0', sizeof(szMyScratch1));
		strcpy((char *)szMyScratch1, (char *)szMyScratch);
	}
    //  strcpy((char*)srISO.pszField63, (char*)szMyScratch);
     inLoadINIRec(0);
if(fGetAddDataFlag() == VS_TRUE)
	{
	if((pobTran->srBRec.inCode == SALE) ||(pobTran->srBRec.inCode == SALE_OFFLINE) ||(pobTran->srBRec.inCode == REFUND) ||
		(pobTran->srBRec.inCode == REFUND_OFFLINE) ||(pobTran->srBRec.inCode == CITI_DEBIT_SALE) ||
	  ( pobTran->srBRec.inCode==CASH_ADV)
		)
	{
	strcpy(srRcpt.unFrm.srReceipt.szGenBuf5,"INF:");
	if(strlen((char *)szMyScratch) >0)
	strcat (srRcpt.unFrm.srReceipt.szGenBuf5, (char *)szMyScratch);
	else
		{
		strcat (srRcpt.unFrm.srReceipt.szGenBuf5, (char *)szMyScratch1);

		}
	}
	}

	strcpy(srRcpt.unFrm.srReceipt.szApprovalCode, pobTran->srBRec.szAuthCode);
    	memset(acct,0,sizeof(acct));
	memset(szPan,0,sizeof(szPan));
	strcpy(acct, pobTran->srBRec.szPAN);
	memset(srRcpt.unFrm.srReceipt.szAcctNum,0x00,sizeof(srRcpt.unFrm.srReceipt.szAcctNum));
	if(pobTran->srBRec.inTranCardType == DEBIT_CARD || pobTran->srBRec.inTranCardType == EBT_CARD) {
		len = strlen(acct);
		strlcpy(szPan,acct,len);
	}
	else
		strcpy(szPan, acct);
	//Kelvin_F2 --- Start: masking PAN by new flags in CPAC
	if (( pobTran->srBRec.inPrintOption == PRT_MERCH && fGetCPACMaskPANinMRcpt())||
		( pobTran->srBRec.inPrintOption == PRT_CUST  && fGetCPACMaskPANinCRcpt())||
		( pobTran->srBRec.inPrintOption == PRT_BANK  && fGetCPACMaskPANinBRcpt())||
		( pobTran->srBRec.inPrintOption == (PRT_MERCH | PRT_DUPLICATE) && fGetCPACMaskPANinMRcpt())||
		( pobTran->srBRec.inPrintOption == (PRT_CUST | PRT_DUPLICATE) && fGetCPACMaskPANinCRcpt())||
		( pobTran->srBRec.inPrintOption == (PRT_BANK | PRT_DUPLICATE) && fGetCPACMaskPANinBRcpt()))
	{
#ifdef HKGM
	memset(srRcpt.unFrm.srReceipt.szAcctNum,0x00,sizeof(srRcpt.unFrm.srReceipt.szAcctNum));
	vdFormatPAN(pobTran, szPan, srRcpt.unFrm.srReceipt.szAcctNum, sizeof (srRcpt.unFrm.srReceipt.szAcctNum));
#else
#if LEN_REMOVED
  vdCPACFormatPAN(szPan, srRcpt.unFrm.srReceipt.szAcctNum); /* Added for Formatting 4444 4444 4444 4444 */
#endif /* LEN_REMOVED */
	vdFormatPAN(pobTran, szPan, srRcpt.unFrm.srReceipt.szAcctNum, sizeof (srRcpt.unFrm.srReceipt.szAcctNum));
#endif /* HKGM */
	}
	else
	{
		//strcpy(srRcpt.unFrm.srReceipt.szAcctNum,szPan);
		vdFormatPAN(pobTran, szPan, srRcpt.unFrm.srReceipt.szAcctNum, sizeof (srRcpt.unFrm.srReceipt.szAcctNum));
	}
	//Kelvin_F2 --- end: masking PAN by new flags in CPAC

	//To print "Chip" as entry mode for chip card transactions
	if(pobTran->srBRec.inChipStatus != NOT_USING_CHIP)
		strcpy(srRcpt.unFrm.srReceipt.szEntryMethod, "DIP");
	else if (pobTran->srBRec.fManual)
		strcpy(srRcpt.unFrm.srReceipt.szEntryMethod,"Manual");
	else
		strcpy(srRcpt.unFrm.srReceipt.szEntryMethod,"Swipe");

	if (pobTran->srBKey.fOffline)
		strcpy(srRcpt.unFrm.srReceipt.szApprovalType,"Offine");
	else
		strcpy(srRcpt.unFrm.srReceipt.szApprovalType,"Online");

	if ( pobTran->srBRec.lnSChgAmount > 0L )
		lnSChg = pobTran->srBRec.lnSChgAmount;

	memset(srRcpt.unFrm.srReceipt.szSChg,0,sizeof(srRcpt.unFrm.srReceipt.szSChg));

	if (pobTran->srBRec.inTranCardType == DEBIT_CARD)
	{
		sprintf(srRcpt.unFrm.srReceipt.szSChg,   SUBSTITUTE_LD, lnSChg);
		vdFormatAmount(srRcpt.unFrm.srReceipt.szSChg, VS_TRUE);

		lnSubTotAmt = (pobTran->srBKey.lnTotalOfTransactionAmount
		               - lnSChg - pobTran->srBRec.lnCashBackAmount);
		sprintf(srRcpt.unFrm.srReceipt.szSubTotAmt, SUBSTITUTE_LD, lnSubTotAmt);
		vdFormatAmount(srRcpt.unFrm.srReceipt.szSubTotAmt, VS_TRUE);

	}

	sprintf(srRcpt.unFrm.srReceipt.szTotAmt, SUBSTITUTE_LD,
	    (pobTran->srBKey.lnTotalOfTransactionAmount));
	vdFormatAmount(srRcpt.unFrm.srReceipt.szTotAmt, VS_TRUE);


	/* if print disclaimer flag is on, propogate disclaimer
	buffers */

	/* LENIN CPAC Disclaimer is hard coded in TPL FIle as per core */

	/** Calculations for suggested tips ****/


	if(fGetSugTipOn() == VS_TRUE)

	/* set default amounts if flag on and field is empty */
	{

	shTempSugTipPer1 = inGetSugTipPer1();

	if(shTempSugTipPer1 <= 0)
	{
	 shTempSugTipPer1 = 1500;
	/* vdSetSugTip1(shTempSugTipPer1);*/
	}

	sprintf(srRcpt.szTipPct1, SUBSTITUTE_D,shTempSugTipPer1);

	//vdPutDecPoint(srRcpt.szTipPct1);  //LENIN ATL
	vdFormatAmount(srRcpt.szTipPct1, VS_TRUE);
	shTempSugTipPer2 = inGetSugTipPer2();

	if(shTempSugTipPer2 <= 0)
	{
	 shTempSugTipPer2 = 2000;
	/*    vdSetSugTip2(shTempSugTipPer2);*/
	}

	sprintf(srRcpt.szTipPct2, SUBSTITUTE_D,shTempSugTipPer2);
	//	   vdPutDecPoint(srRcpt.szTipPct2);//LENIN ATL
	vdFormatAmount(srRcpt.szTipPct2, VS_TRUE);
	shTempSugTipPer3 = inGetSugTipPer3();

	if(shTempSugTipPer3 <= 0)
	{
	 shTempSugTipPer3 = 2500;
	/*         vdSetSugTip3(shTempSugTipPer3);*/
	}


	sprintf(srRcpt.szTipPct3, SUBSTITUTE_D,shTempSugTipPer3);
	// vdPutDecPoint(srRcpt.szTipPct3);//LENIN ATL
	vdFormatAmount(srRcpt.szTipPct3, VS_TRUE);

	lnTipAmt1 = ((pobTran->srBRec.lnBaseTransactionAmount / 10000L) * shTempSugTipPer1);
	lnTipAmt1 += (((pobTran->srBRec.lnBaseTransactionAmount % 10000L) * shTempSugTipPer1) / 10000L);

	/*ComputePercent(shTempSugTipPer1,pobTran->srBRec.lnBaseTransactionAmount,lnTipAmt1);*/
	sprintf(srRcpt.szTipAmt1, SUBSTITUTE_LD,lnTipAmt1);
	//        vdPutDecPoint(srRcpt.szTipAmt1);//LENIN ATL
	vdFormatAmount(srRcpt.szTipAmt1, VS_TRUE);

	lnTipAmt2 = ((pobTran->srBRec.lnBaseTransactionAmount / 10000L) * shTempSugTipPer2);
	lnTipAmt2 += (((pobTran->srBRec.lnBaseTransactionAmount % 10000L) * shTempSugTipPer2) / 10000L);

	/*ComputePercent(shTempSugTipPer2,pobTran->srBRec.lnBaseTransactionAmount,lnTipAmt2);*/
	sprintf(srRcpt.szTipAmt2, SUBSTITUTE_LD,lnTipAmt2);
	//        vdPutDecPoint(srRcpt.szTipAmt2);//LENIN ATL
	vdFormatAmount(srRcpt.szTipAmt2, VS_TRUE);

	lnTipAmt3 = ((pobTran->srBRec.lnBaseTransactionAmount / 10000L) * shTempSugTipPer3);
	lnTipAmt3 += (((pobTran->srBRec.lnBaseTransactionAmount % 10000L) * shTempSugTipPer3) / 10000L);

	/*ComputePercent(shTempSugTipPer3,pobTran->srBRec.lnBaseTransactionAmount,lnTipAmt3);*/
	sprintf(srRcpt.szTipAmt3, SUBSTITUTE_LD,lnTipAmt3);
	//        vdPutDecPoint(srRcpt.szTipAmt3);//LENIN ATL
	vdFormatAmount(srRcpt.szTipAmt3, VS_TRUE);
	}

	sprintf(srRcpt.unFrm.srReceipt.szTipAmt, SUBSTITUTE_LD, pobTran->srBRec.lnTipAmount);
	sprintf(srRcpt.unFrm.srReceipt.szBaseAmt, SUBSTITUTE_LD, pobTran->srBRec.lnBaseTransactionAmount);

	/* Saritha_b1.......Start........Printing Cash Back Amount on the receipt for Cash Back transaction */
	sprintf(srRcpt.unFrm.srReceipt.szCBack, SUBSTITUTE_LD, pobTran->srBRec.lnCashBackAmount);

	/* Saritha_b1.......Start........Printing Cash Back Amount on the receipt for Cash Back transaction */

	if (pobTran->srBRec.inPrintOption & PRT_DUPLICATE)
	{
	lnBaseAmt = (pobTran->srBKey.lnTotalOfTransactionAmount
	                   - pobTran->srBRec.lnTaxAmount - pobTran->srBRec.lnTipAmount -
	                     pobTran->srBRec.lnCashBackAmount - lnSChg);
	sprintf(srRcpt.unFrm.srReceipt.szBaseAmt, SUBSTITUTE_LD, lnBaseAmt);
	}
	else
	{
	if ( pobTran->srBKey.fVoided )
	{
	    sprintf (srRcpt.unFrm.srReceipt.szTotAmt, SUBSTITUTE_LD,
	             pobTran->srBKey.lnTotalOfTransactionAmount);
	    vdFormatAmount(srRcpt.unFrm.srReceipt.szTotAmt, VS_TRUE);
	    lnBaseAmt = (pobTran->srBKey.lnTotalOfTransactionAmount
	                   - pobTran->srBRec.lnTaxAmount - pobTran->srBRec.lnTipAmount -
	                     pobTran->srBRec.lnCashBackAmount - lnSChg);
	    sprintf(srRcpt.unFrm.srReceipt.szBaseAmt, SUBSTITUTE_LD, lnBaseAmt);
	}
	if (pobTran->fAdjust)
	{
	//sprintf (srRcpt.unFrm.srReceipt.szGenBuf1, SUBSTITUTE_LD,
	 //            (pobTran->srBRec.lnOrigTotalTransactionAmount-pobTran->srBRec.lnOldTipAmount));
	sprintf (srRcpt.unFrm.srReceipt.szGenBuf1, SUBSTITUTE_LD,
	             (pobTran->srBRec.lnOldTotalTransactionAmount-pobTran->srBRec.lnOldTipAmount));




	  /*  vdFormatAmount(srRcpt.unFrm.srReceipt.szBaseAmt);  Base Amount has the old Amount. Lenin 13/04/2000 */
	 sprintf (srRcpt.unFrm.srReceipt.szBaseAmt, SUBSTITUTE_LD,
	             pobTran->srBRec.lnBaseTransactionAmount);

	}
	}
/* LIEM COMMENT
	if(inGGCode() == PRE_AUTH)
	//Kelvin_F2 --- start: format the amount field for pre_auth
	#ifndef HKGM
	sprintf(srRcpt.unFrm.srReceipt.szBaseAmt, "%06.6ld", pobTran->srBRec.lnBaseTransactionAmount);
	#endif
	#ifdef HKGM
	{
	sprintf(srRcpt.unFrm.srReceipt.szBaseAmt, SUBSTITUTE_LD, pobTran->srBRec.lnBaseTransactionAmount);
	vdFormatAmount(srRcpt.unFrm.srReceipt.szBaseAmt, VS_TRUE);
	}
	#endif
	//Kelvin_F2 --- end: format the amount field for pre_auth
	else
*/
	vdFormatAmount(srRcpt.unFrm.srReceipt.szBaseAmt, VS_TRUE);
	vdFormatAmount(srRcpt.unFrm.srReceipt.szTipAmt, VS_TRUE);
	 vdFormatAmount(srRcpt.unFrm.srReceipt.szGenBuf1, VS_TRUE);

	     /* Saritha_b1.......Start........Printing Cash Back Amount on the receipt for Cash Back transaction */

	vdFormatAmount(srRcpt.unFrm.srReceipt.szCBack, VS_TRUE);

	   /* Saritha_b1.......Start........Printing Cash Back Amount on the receipt for Cash Back transaction */
}
